var action = null;
var formhtml = null;
function plugin_comments_init() {
  function delete_all_forms() { jQuery('__cid__ div.plugin_comments .reply').html(''); }
  function capture_form() {
     jQuery('__cid__ div.plugin_comments #close').click(function(){
       delete_all_forms();
     });
    jQuery('__cid__ div.plugin_comments :submit').click(function(){
      var form = jQuery(this).parent()
      var body = form.children('textarea[name="body"]').val();
       jQuery.post(action+'/'+form.parent().prev().attr('id'),
                  'body='+encodeURIComponent(body),
                   function(data,r){ if(data) { form.parent().next().prepend(data);
                                                form.children('textarea[name="body"]').val('')
                                                delete_all_forms();
                                                plugin_comments_init(); }
                   },'html');
      return false;
    });
  }
  jQuery('__cid__ div.plugin_comments #toggle').click(function(){
     jQuery(this).parent().next().next().slideToggle();
  });
  jQuery('__cid__ div.plugin_comments #reply').click(function(){
     delete_all_forms();
     jQuery(this).parent().next().next().slideDown();
     jQuery(this).parent().next().html(formhtml); capture_form(); 
  });
  jQuery('__cid__ div.plugin_comments #delete').click(function(){
    delete_all_forms();
    var parent = jQuery(this).parent()
    jQuery.post(action+'/delete/'+parent.attr('id'),null,function(data,r){parent.html('deleted');});
  });
}
jQuery(document).ready(function() {
  action = jQuery('__cid__ div.plugin_comments form').attr('action');  
  formhtml = jQuery('__cid__ div.plugin_comments form').parent().html();
  jQuery('__cid__ div.plugin_comments #toggle').parent().next().next().hide();
  jQuery('__cid__ div.plugin_comments .reply').html('');
  plugin_comments_init()}
);
